package com.ymatou.iapi.csim.testcase;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.csim.parameter.GetMessageListBean;
import com.ymatou.iapi.csim.parameter.GetUserMessageListResp;
import com.ymatou.iapi.csim.parameter.SendMessageBean;
import com.ymatou.iapi.csim.service.GetMessageListCall;
import com.ymatou.iapi.csim.service.RevolveSessionJOBCall;
import com.ymatou.iapi.csim.service.SendMessageCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymatou.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CsimCallService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.YmtMessage;
import com.ymttest.database.model.ymt_cssession;
import com.ymttest.database.model.ymt_onlinecs;
import com.ymttest.database.sqlwapper.ImDefaultWapper;
import com.ymttest.database.sqlwapper.CSIM.ymt_onlinecsWapper;
import com.ymttest.database.sqlwapper.CSIM.ymt_queuesessionWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;
/**
*获取消息列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SendMessage{
	private static SendMessageBean bean;
	private static SendMessageCall call;
	private Integer csIdA=Integer.parseInt(TestDataManager.getRandomNum(5));
	private RevolveSessionJOBCall jobCall=new RevolveSessionJOBCall();
	private Integer userId=Integer.parseInt(EnvSetup.getData("userId_IM").toString());
	private String userName=EnvSetup.getData("userName_IM").toString();
	
	private Integer sellerId1=Integer.parseInt(EnvSetup.getData("autotest_seller1").toString());
	private String sellerName1=EnvSetup.getData("autotest_sellerName1").toString();
	private static ymt_onlinecsWapper onlinecsWapper=new ymt_onlinecsWapper();
	private static ymt_queuesessionWapper queuesessionWapper=new ymt_queuesessionWapper();
	private CsimCallService service=new CsimCallService();
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("买家和买手对客户发消息");
	}

	@Before
	public void caseUp() {
		bean=new SendMessageBean();
		call=new SendMessageCall();
		queuesessionWapper.deleteAll();
		onlinecsWapper.deleteBy(MapUtil.hashMap("CSId",userId,"Status",0));
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessage_001() {
		Logger.start(true,"用户发消息-买家给码头洋洋发消息");
		try {
			//csIdA上线
			ymt_onlinecs onlinecs=service.getOnlineCS(csIdA);
			//csIdA 主动会话
			ymt_cssession csSession=service.getOpenSession(csIdA,userId);
			csIdA=csSession.getCSId();//这里给csidA赋值 目的为了，当前的用户已经在会话中，直接把当前的csId赋值给csIdA
			
		    bean.setUserId(userId);
            bean.setMessage("自动化测试_"+TestDataManager.getRandomIntNum(5));	
            bean.setMessageSource(1);
            bean.setSourceParams("App");
            bean.setCallType(0);//0普通用户 1专属用户
            bean.setQuestionGuideId(1);
            bean.setAppId("imyy.ymatou.com");
			call.setData(bean);
			Thread.sleep(2000);
			jobCall.callService();
			 
//			       for(int i=0; i<5000; i++){
//			 
//			          new Thread("" + i){
//			 
//			             public void run(){
//			 
//			     			try {
								call.callService();
//							} catch (IOException e) {
//								e.printStackTrace();
//							}	
//			             }
//			          }.start();
//			 
//			       }


			
			verifyDate(userId,userName);
			Logger.comment("call.getData().getMessageId():"+call.getData().getMessageId());

			//Logger.debug(FormatUtil.beanToGSONString(im.selectImInfo(call.getData().getMessageId())));
			
			
//			GetMessageListBean getmessageListBean=csService.GetMessageListBean();
//			getmessageListBean.setToUserId(100);
//			getmessageListBean.setIsNew(true);
//
//			getmessageListBean.setSessionNumber("20181016140755_1128_100");
//			getmessageListBean.setCallType(0);
//			csService.GetMessageListCall(getmessageListBean);
			
			
//			GetMessageListBean getmessageListBean=csService.GetMessageListBean();
//			getmessageListBean.setFromuserId(100);
//			getmessageListBean.setToUserId(20124241);
//			getmessageListBean.setIsNew(true);
//
//			getmessageListBean.setSessionNumber("20181016140755_1128_100");
//			getmessageListBean.setCallType(0);
//			csService.GetMessageListCall(getmessageListBean);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessage_001_1() {
		Logger.start(true,"用户发消息-买手给码头洋洋发消息-CallType=1表示AM客服");
		try {
			
		    bean.setUserId(userId);
            bean.setMessage("自动化测试_"+TestDataManager.getRandomIntNum(5));
            bean.setMessageSource(1);
            bean.setSourceParams("App");
            bean.setCallType(1);
            bean.setQuestionGuideId(0);
            bean.setAppId("imyy.ymatou.com");
			call.setData(bean);
			call.callService();
			
			Thread.sleep(2000);
			jobCall.callService();
			
			verifyDate(userId,userName);
			Logger.comment("call.getData().getMessageId():"+call.getData().getMessageId());

			Logger.debug(FormatUtil.beanToGSONString(new ImDefaultWapper().selectImInfo(call.getData().getMessageId())));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessage_002() {
		Logger.start(true,"用户发消息-买手给码头洋洋发消息-CallType=1表示AM客服,返回错误");
		try {
			
		    bean.setUserId(sellerId1);
            bean.setMessage("自动化测试_"+TestDataManager.getRandomIntNum(5));
            bean.setMessageSource(1);
            bean.setSourceParams("App");
            bean.setCallType(0);
            bean.setQuestionGuideId(1);
            bean.setAppId("imyy.ymatou.com");
			call.setData(bean);
			call.callService();	
			
			Thread.sleep(2000);
			jobCall.callService();
			
			verifyDate(sellerId1,sellerName1);
			Logger.comment("call.getData().getMessageId():"+call.getData().getMessageId());

			//Logger.debug(FormatUtil.beanToGSONString(im.selectImInfo(call.getData().getMessageId())));
		} catch (Exception e) {
			Logger.fail(e);	
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessage_002_1() {
		Logger.start(true,"用户发消息-买手给码头洋洋发消息");
		try {
			
		    bean.setUserId(sellerId1);
            bean.setMessage("自动化测试_"+TestDataManager.getRandomIntNum(5));
            bean.setMessageSource(1);
            bean.setSourceParams("App");
            bean.setCallType(1);
            bean.setQuestionGuideId(0);
            bean.setAppId("imyy.ymatou.com");
			call.setData(bean);
			call.callService();		
			verifyDate(sellerId1,sellerName1);
			Logger.comment("call.getData().getMessageId():"+call.getData().getMessageId());
//	    	Logger.verifyEquals("500",call.getStatus(), "验证返回Status=500");
//	    	Logger.verifyEquals(true,call.getMsg().contains("此系统没有在可访问系统中"), "验证返回Msg");

			//Logger.debug(FormatUtil.beanToGSONString(im.selectImInfo(call.getData().getMessageId())));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessage_003() {
		Logger.start(true,"用户发消息-消息内容含有特殊字符,如:&");
		try {
		    bean.setUserId(userId);
            bean.setMessage("自动化测试_"+TestDataManager.getRandomIntNum(5)+"&");
            bean.setMessageSource(1);
            bean.setSourceParams("App");
            bean.setCallType(0);
            bean.setQuestionGuideId(1);
            bean.setAppId("imyy.ymatou.com");
			call.setData(bean);
			call.callService();			
			verifyDate(userId,userName);
			Logger.debug(FormatUtil.beanToGSONString(new ImDefaultWapper().selectImInfo(call.getData().getMessageId())));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessage_004() {
		Logger.start(true,"用户发消息-消息内容含有sql注入关键字符,如:or、update、insert");
		try {
			bean.setUserId(userId);
            bean.setMessage("自动化测试_"+TestDataManager.getRandomIntNum(5)+"1 or 1 update insert");
            bean.setMessageSource(1);
            bean.setSourceParams("App");
            bean.setCallType(0);
            bean.setQuestionGuideId(1);
            bean.setAppId("imyy.ymatou.com");
			call.setData(bean);
			call.callService();			
			verifyDate(userId,userName);
			Logger.debug(FormatUtil.beanToGSONString(new ImDefaultWapper().selectImInfo(call.getData().getMessageId())));		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_SendMessage_005() {
		Logger.start(true,"用户发消息-userId不存在");
		try {
		    bean.setUserId(111111111);
            bean.setMessage("自动化测试_"+TestDataManager.getRandomIntNum(5));
            bean.setMessageSource(1);
            bean.setSourceParams("App");
            bean.setCallType(0);//0普通用户 1专属用户
            bean.setQuestionGuideId(1);
            bean.setAppId("imyy.ymatou.com");
			call.setData(bean);
			call.callService();	
			verifyDate(bean.getUserId(),sellerName1);
			Logger.comment("call.getData().getMessageId():"+call.getData().getMessageId());
//	    	Logger.verifyEquals("500",call.getStatus(), "验证返回Status=500");
//	    	Logger.verifyEquals(true,call.getMsg().contains("此系统没有在可访问系统中"), "验证返回Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_SendMessage_006() {
		Logger.start(true,"用户发消息-message为空");
		try {
		    bean.setUserId(userId);
           // bean.setMessage("自动化测试_"+TestDataManager.getRandomIntNum(5));
            bean.setMessageSource(1);
            bean.setSourceParams("App");
            bean.setCallType(0);//0普通用户 1专属用户
            bean.setQuestionGuideId(1);
            bean.setAppId("imyy.ymatou.com");
			call.setData(bean);
			call.callService();	
			verifyDate(userId,userName);
			Logger.comment("call.getData().getMessageId():"+call.getData().getMessageId());
//	    	Logger.verifyEquals("500",call.getStatus(), "验证返回Status=500");
//	    	Logger.verifyEquals(true,call.getMsg().contains("此系统没有在可访问系统中"), "验证返回Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessage_007() {
		Logger.start(true,"用户发消息-SourceParams为空");
		try {
		    bean.setUserId(userId);
            bean.setMessage("自动化测试_"+TestDataManager.getRandomIntNum(5));
            bean.setMessageSource(1);
           // bean.setSourceParams("App");
            bean.setCallType(0);//0普通用户 1专属用户
            bean.setQuestionGuideId(1);
            bean.setAppId("imyy.ymatou.com");
			call.setData(bean);
			call.callService();	
			Logger.debug(FormatUtil.beanToGSONString(new ImDefaultWapper().selectImInfo(call.getData().getMessageId())));	
			verifyDate(userId,userName);
			Logger.comment("call.getData().getMessageId():"+call.getData().getMessageId());
//	    	Logger.verifyEquals("500",call.getStatus(), "验证返回Status=500");
//	    	Logger.verifyEquals(true,call.getMsg().contains("此系统没有在可访问系统中"), "验证返回Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessage_008() {
		Logger.start(true,"用户发消息-SourceParams为空");
		try {
		    bean.setUserId(userId);
            bean.setMessage("自动化测试_"+TestDataManager.getRandomIntNum(5));
            bean.setMessageSource(1);
           // bean.setSourceParams("App");
            bean.setCallType(0);//0普通用户 1专属用户
            bean.setQuestionGuideId(1);
            bean.setAppId("imyy.ymatou.com");
			call.setData(bean);
			call.callService();	
			Logger.debug(FormatUtil.beanToGSONString(new ImDefaultWapper().selectImInfo(call.getData().getMessageId())));
			verifyDate(userId,userName);
			Logger.comment("call.getData().getMessageId():"+call.getData().getMessageId());
//	    	Logger.verifyEquals("500",call.getStatus(), "验证返回Status=500");
//	    	Logger.verifyEquals(true,call.getMsg().contains("此系统没有在可访问系统中"), "验证返回Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendMessage_009() {
		Logger.start(true,"用户发消息-买家给码头洋洋发消息");
		try {
//			//csIdA上线
//			ymt_onlinecs onlinecs=service.getOnlineCS(csIdA);
			
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer uid = uc.getUserid();
			String token=uc.getAccessToken();
//			//csIdA 主动会话
			ymt_cssession csSession=service.getOpenSession(1128,uid);

		    csIdA=csSession.getCSId();//这里给csidA赋值 目的为了，当前的用户已经在会话中，直接把当前的csId赋值给csIdA
			
		    bean.setUserId(uid);
            bean.setMessage("自动化测试_"+TestDataManager.getRandomIntNum(5));	
            bean.setMessageSource(1);
            bean.setSourceParams("App");
            bean.setCallType(0);//0普通用户 1专属用户
            bean.setQuestionGuideId(1);
            bean.setAppId("imyy.ymatou.com");
			call.setData(bean);
			call.callService();
			Thread.sleep(2000);
			jobCall.callService();
			
//			//获取消息
//			GetMessageListBean getMessageListBean=new CsimCallService().GetMessageListBean();
//			getMessageListBean.setFromuserId(csIdA);
//			getMessageListBean.setToUserId(uid);
//			
//			GetMessageListCall messageCall=new CsimCallService().GetMessageListCall(getMessageListBean);
//			GetUserMessageListResp messageResp=messageCall.getData();
//			messageResp.getYmtMessageList();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
    private void verifyDate(int id,String name) throws InterruptedException
    {
    	//验证code
    	Logger.verifyEquals("200",call.getStatus(), "验证返回Status=200");
    	Logger.verifyEquals("",call.getMsg(), "验证返回Msg");
    	ImDefaultWapper im=new ImDefaultWapper();
		YmtMessage message=im.selectImInfo(call.getData().getMessageId()).get(0);
		Logger.verifyEquals(id, message.getMsgFromId(), "验证返回的MsgFromId与数据库是否一致");
		Logger.verifyEquals(name, message.getMsgFrom(), "验证返回的MsgFrom与数据库是否一致");
		Logger.verifyEquals(bean.getMessage(), message.getMessage(), "验证返回的message与数据库是否一致");
		//发送一条消息，首先与固定客服 msgToId=20124241 建立连接，然后在分配给具体客服csId=20124242
		Logger.verifyEquals(20124241, message.getMsgToId(), "验证返回的MsgToId与数据库是否一致");
	//	Logger.verifyEquals(userId, message.getMsgToId(), "验证返回的MsgToId与数据库是否一致");
		//Logger.verifyEquals(20124242, message.getCsId(), "验证返回的CsId与数据库是否一致");
		Logger.verifyEquals(csIdA, message.getCsId(), "验证返回的CsId与数据库是否一致");
		//Logger.verifyEquals("临时客服", message.getCsName(), "验证返回的CsName与数据库是否一致");
		Logger.verifyEquals("买家客服1", message.getCsName(), "验证返回的CsName与数据库是否一致");
		Logger.verifyNotNull( message.getSessionNumber(), "验证数据库中的SessionNumber不为空");
    }
}